<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Amitav Roy
 * Date: 1/26/13
 * Time: 4:35 PM
 * To change this template use File | Settings | File Templates.
 */
class Page_model extends CI_Model {
  public function __construct() {
    parent::__construct();
  }

  // this is the public function to build the landing page
  public function get_homepage_nodes($page = null) {
    return $this->latest_homepage_nodes($page);
  }

  // getting the list of node ids which will be lo
  protected function latest_homepage_nodes($page) {
    $this->db->select('nid'); // fetching field nid

    $this->db->where('status', 1); // only published articles
    $this->db->where('type', 'article'); // only articles

    $this->db->order_by('created', 'DESC');

    if ($page) {
      $this->db->limit(10, $page);
    } else {
      $this->db->limit(10);
    }

    $query = $this->db->get('node'); // fire the query

    $result = $query->result_array();

    // loading the node model
    $this->load->model('nodes/node_model', 'node');
    //dd($result);
    foreach ($result as $data) {
      $nodes[] = $this->node->node_load($data['nid']);
    }

    if ($nodes)
      return $nodes;
    else
      return false;

  }

  // this is the public function to get the nodes for web
  public function get_webarticles_nodes($page = null) {
    return $this->latest_webpage_nodes($page);
  }

  // this is where I am building the nodes for Web articles page.
  protected function latest_webpage_nodes($page) {
    $tid = 3;

    $this->db->from('node n');

    $this->db->join('taxonomy_index ti', 'ti.nid=n.nid');

    $this->db->where('ti.tid', $tid);
    $this->db->order_by('n.created','desc');

    if ($page) {
      $this->db->limit(10, $page);
    } else {
      $this->db->limit(10);
    }

    $this->db->select('n.nid');

    $query = $this->db->get();
    if ($query->num_rows > 0) {
      $this->load->model('nodes/node_model', 'node');
      $query = $query->result_array();
      foreach ($query as $record) {
        $nodes[] = $this->node->node_load($record['nid']);
      }
      return $nodes;
    }
    else {
      return false;
    }

  }

  // public function to get the nodes for the travel page
  public function get_travel_articles($page = null) {
    return $this->latest_travelpage_nodes($page);
  }

  // this is where I am building the nodes for travel page.
  protected function latest_travelpage_nodes($page) {
    $tid = 2;

    $this->db->from('node n');

    $this->db->join('taxonomy_index ti', 'ti.nid=n.nid');

    $this->db->where('ti.tid', $tid);
    $this->db->order_by('n.created','desc');

    if ($page) {
      $this->db->limit(10, $page);
    } else {
      $this->db->limit(10);
    }

    $this->db->select('n.nid');

    $query = $this->db->get();
    if ($query->num_rows > 0) {
      $this->load->model('nodes/node_model', 'node');
      $query = $query->result_array();
      foreach ($query as $record) {
        $nodes[] = $this->node->node_load($record['nid']);
      }
      return $nodes;
    }
    else {
      return false;
    }
  }

  // this function takes the section id and returns the 
  // number of articles in that section. Req for pager
  public function get_sec_article_count($section = 'all') {
    $this->db->select();
    $this->db->from('node n');
    $this->db->join('taxonomy_index ti', 'ti.nid=n.nid');
    $this->db->join('taxonomy_term_data td', 'td.tid=ti.tid');
    if ($section != 'all') {
      $this->db->where('td.tid',$section);
    }
    $query = $this->db->get();
    //$this->db->last_query();
    return $query->num_rows();
  }

  /**
   * This public function gives the array of nodes based on the term id
   * and takes pager as argument if present
   */
  public function get_term_based_nodes($tid, $page = null) {
    return $this->latest_articles_based_on_term($tid, $page);
  }

  // the protected function to generate the array of nodes.
  protected function latest_articles_based_on_term($tid, $page) {
    $this->db->from('node n');

    $this->db->join('taxonomy_index ti', 'ti.nid=n.nid');

    $this->db->where('ti.tid', $tid);
    $this->db->order_by('n.created','desc');

    if ($page) {
      $this->db->limit(10, $page);
    } else {
      $this->db->limit(10);
    }

    $this->db->select('n.nid');

    $query = $this->db->get();

    return $query->result_array();
  }

  public function term_based_article_count($tid) {
    $this->db->from('node n');

    $this->db->join('taxonomy_index ti', 'ti.nid=n.nid');

    $this->db->where('ti.tid', $tid);
    $this->db->order_by('n.created','desc');

    $this->db->select('n.nid');

    $query = $this->db->get();

    return $query->num_rows();
  }
}